/*
 *  This file is part of Social Media Monitoring Toolbox.
 *
 *  Social Media Monitoring Toolbox is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Social Media Monitoring Toolbox is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Social Media Monitoring Toolbox. If not, see <http://www.gnu.org/licenses/lgpl.html>.
 *
 * � Copyright 2013
 * Jana Asmussen, Julian Bau�, Daniela Dalock, Christian Gutjahr,
 * Fabian Heidorn, Alexander Kaack, Vitali Kagadij 
 * 
 */

package gui;

import gui.Lazy;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import org.primefaces.model.LazyDataModel;
import types.StreamDescriptor;
import types.TableTyp;
import types.TweetPost;
import types.YoutubeVideoType;

@ManagedBean(name = "listBean")
@ViewScoped
public class ListBean implements Serializable {

	private static final long serialVersionUID = -950533761370819199L;
	public StreamDescriptor stream;
	private GetData data = new GetData();
	private List<TableTyp> top;
	private LazyDataModel<TableTyp> lazyModel;
	private Map<TableTyp,String> views = new HashMap<TableTyp,String>();
	
	public ListBean() {
		createTopList();
	}

	/**
	 * Update the lazy module
	 */
	public void updateLazyModelTop() {
		lazyModel = null;
		lazyModel = new Lazy(this.top);
	}

	/**
	 * Create the top list for the youtube clips
	 */
	public void createTopList() {
		int count = 1;
		this.top = new ArrayList<TableTyp>();
		
		List<YoutubeVideoType> youtube = data.getTopYoutubeStreams();
		for (YoutubeVideoType clip : youtube) {
			if (!this.top.contains(clip.getVideoTitle())){
				TableTyp tableEntry = new TableTyp();
				tableEntry.setId(count++);
				tableEntry.setName(clip.getVideoTitle());
				this.top.add(tableEntry);
				this.views.put(tableEntry, Long.toString(clip.getViews()));
			}
		}
		updateLazyModelTop();
	}

	/**
	 * Getter of the top list
	 * @return the top list
	 */
	public List<TableTyp> getTop() {
		return top;
	}

	/**
	 * Setter of the top list
	 * @param top the sorted list of top posts
	 */
	public void setTop(List<TableTyp> top) {
		this.top = top;
	}
	
	/**
	 * Getter of the lazy module
	 * @return the lazymodule
	 */
	public LazyDataModel<TableTyp> getLazyModel() {
		return lazyModel;
	}
	
	/**
	 * Getting the views of a clip
	 * @param clip
	 * @return returns the views of a clip
	 */
	public String getViews(TableTyp clip){
		return this.views.get(clip);		
	}
	
}
